package com.example.pjy.controller;

import com.example.pjy.pojo.Academic;
import com.example.pjy.pojo.Result;
import com.example.pjy.pojo.TotalScore;
import com.example.pjy.service.TotalScoreService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/totalScore")
public class TotalScoreController {
    @Resource
    private TotalScoreService totalScoreService;


//    @RequestMapping("/insert")//url路径叫insert
//    public String insert(Total_Score total_score){
//        total_scoreService.insert(total_score);
//        return "success";
//    }
//
//    @RequestMapping("/deleteById")
//    public String deleteById(int student_id){
//        total_scoreService.deleteById(student_id);
//        return "success";
//    }
//
//    @RequestMapping("/update")
//    public String update(Total_Score total_score){
//        total_scoreService.update(total_score);
//        return "success";
//    }
//
//    @RequestMapping("/selectAll")
//    public List<Total_Score> selectAll(){
//        return total_scoreService.selectAll();
//    }
//
//    @RequestMapping("/selectById")
//    public Total_Score selectById(int student_id){
//        return total_scoreService.selectById(student_id);
//    }



    //把接收到的json转为实体类对象 运行时给的是json
    @PostMapping("/add")
    public Result add(@RequestBody TotalScore totalScore) {
        totalScoreService.add(totalScore);
        return Result.success();
    }


    //查询 有主体数据给浏览器响应 主题数据是数组 数组里有多个对象
    @GetMapping("/selectlist")
    public Result<List<TotalScore>> list(){
        List<TotalScore> totalScore= totalScoreService.list();
        return Result.success(totalScore);//结果是数组
    }


    //根据id找学生
    @GetMapping("/detail")
    public Result<TotalScore> detail(Integer studentId){//student响应给前端浏览器是json  id接收前端传来的id
        TotalScore totalScore = totalScoreService.findById(studentId);
        return Result.success(totalScore);//结果是对象
    }


    //把json转为对象
    @PutMapping("/updateto")
    public Result updateto(@RequestBody TotalScore totalScore){
        totalScoreService.updateto(totalScore);
        return Result.success();
    }


    @DeleteMapping("/delete/{studentId}")
    public Result delete(@PathVariable("studentId") Integer studentId){
        if (studentId == null) {
            return Result.error("学生ID不能为空");
        }
        totalScoreService.delete(studentId);
        return Result.success("删除成功");
    }
}
